const mysql = require("mysql");

// 数据库配置
const config = {
  host: "rm-2ze6c2p248id08490ko.mysql.rds.aliyuncs.com",
  user: "root",
  password: "Ksdhy666",
  database: "shop",
  dateStrings: true,
};

// 创建数据库连接池
const pool = mysql.createPool(config);

// 封装数据库查询操作
const query = (sql, params) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        return reject(err);
      }
      connection.query(sql, params, (error, results, fields) => {
        // 释放连接
        connection.release();
        if (error) {
          return reject(error);
        }
        resolve(results);
      });
    });
  });
};

module.exports = {
  query,
};
